<?php


namespace app\api\controller;

use app\data\model\DataNoticePushLog;
use app\data\service\NoticeService;
use think\admin\Controller;

class Notice extends Controller
{
    public function list()
    {
        $userId = input('user_id');
        $type   = input('type', '', 'string');
        $page   = input('page', 1, 'int');
        $limit  = input('limit', 15, 'int');
        try {
            $list = [];
            if ($userId) {
                $list     = NoticeService::instance()->getUserNoticeList($userId, $type, $page, $limit);
                $total    = $list->total();
                $lastPage = $list->lastPage();
                $item     = $list->items();
                $list     = $item;
            }
        }
        catch (\Exception $e) {
            return_error($e->getMessage());
        }

        return_msg(200, 'success', [
            'total'     => $total ?? 0,
            'list'      => $list,
            'last_page' => $lastPage ?? 0,
        ]);
    }

    public function info()
    {
        $userId   = input('user_id', 0, 'int');
        $noticeId = input('notice_id', 0, 'int');
        try {
            if (!$noticeId) {
                throw new \Exception('参数错误');
            }
            /**
             * @var DataNoticePushLog $info
             */
            $info = NoticeService::instance()->getOne($userId, $noticeId);
            if (!$info) {
                throw new \Exception('通知不存在');
            }
            return_msg(200, 'success', $info);
        }
        catch (\Exception $e) {
            return_error($e->getMessage());
        }
    }

    public function batchRead()
    {
        $userId = input('user_id', 0, 'int');
        try {
            if (!$userId) {
                throw new \Exception('参数错误');
            }
            NoticeService::instance()->batchNoticeRead($userId);
            return_msg(200, 'success');
        }
        catch (\Exception $e) {
            return_error($e->getMessage());
        }
    }
}
